package com.wang.blog.controller;

import com.alibaba.fastjson.support.spring.annotation.FastJsonFilter;
import com.alibaba.fastjson.support.spring.annotation.FastJsonView;

import com.wang.blog.constant.ResultCode;
import com.wang.blog.model.MeComment;
import com.wang.blog.model.SysUser;
import com.wang.blog.result.Result;
import com.wang.blog.service.CommentService;
import com.wang.blog.service.impl.CommentServiceImpl;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 评论api
 *
 * @author shimh
 * <p>
 * 2018年1月25日
 */
@RestController
@RequestMapping(value = "/comments")
public class CommentController {

    @Autowired
    private CommentService commentService;


//    @GetMapping
//    public Result listComments() {
//        List<Comment> comments = commentService.findAll();
//
//        return Result.success(comments);
//    }

//    @GetMapping("/{id}")
//    @LogAnnotation(module = "评论", operation = "根据id获取评论")
//    public Result getCommentById(@PathVariable("id") Integer id) {
//
//        Result r = new Result();
//
//        if (null == id) {
//            r.setResultCode(ResultCode.PARAM_IS_BLANK);
//            return r;
//        }
//
//        Comment comment = commentService.getCommentById(id);
//
//        r.setResultCode(ResultCode.SUCCESS);
//        r.setData(comment);
//        return r;
//    }

    @GetMapping("/article/{id}")
    @FastJsonView(
            exclude = {
                    @FastJsonFilter(clazz = MeComment.class, props = {"article", "parent"})},
            include = {@FastJsonFilter(clazz = SysUser.class, props = {"id", "nickname", "avatar"})})

    public Result listCommentsByArticle(@PathVariable("id") Integer id) {

        Result r = new Result();

        if (null == id) {
            r.setResultCode(ResultCode.PARAM_IS_BLANK);
            return r;
        }

        List<MeComment> comments = commentService.listCommentsByArticle(id);

        r.setResultCode(ResultCode.SUCCESS);
        r.setData(comments);
        return r;
    }


//    @PostMapping("/create")
//    @RequiresAuthentication
//    @LogAnnotation(module = "评论", operation = "添加评论")
//    public Result saveComment(@Validated @RequestBody Comment comment) {
//
//        Integer commentId = commentService.saveComment(comment);
//
//        Result r = Result.success();
//        r.simple().put("commentId", commentId);
//        return r;
//    }
//
//
//    @GetMapping("/delete/{id}")
//    @RequiresAuthentication
//    @LogAnnotation(module = "评论", operation = "删除评论")
//    public Result deleteCommentById(@PathVariable("id") Integer id) {
//        Result r = new Result();
//
//        if (null == id) {
//            r.setResultCode(ResultCode.PARAM_IS_BLANK);
//            return r;
//        }
//
//        commentService.deleteCommentById(id);
//
//        r.setResultCode(ResultCode.SUCCESS);
//        return r;
//    }
//
    @PostMapping("/create/change")
    @FastJsonView(
            exclude = {
                    @FastJsonFilter(clazz = MeComment.class, props = {"article"})},
            include = {@FastJsonFilter(clazz = SysUser.class, props = {"id", "nickname", "avatar"})})
    @RequiresAuthentication

    public Result saveCommentAndChangeCounts(@RequestBody MeComment comment) {


        MeComment savedComment = commentService.saveCommentAndChangeCounts(comment);

        Result r = Result.success(savedComment);
        return r;
    }

//
//    @GetMapping("/delete/change/{id}")
//    @RequiresAuthentication
//    @LogAnnotation(module = "评论", operation = "删除评论，减少评论数")
//    public Result deleteCommentByIdAndChangeCounts(@PathVariable("id") Integer id) {
//        Result r = new Result();
//
//        if (null == id) {
//            r.setResultCode(ResultCode.PARAM_IS_BLANK);
//            return r;
//        }
//
//        commentService.deleteCommentByIdAndChangeCounts(id);
//
//        r.setResultCode(ResultCode.SUCCESS);
//        return r;
//    }

}
